﻿Imports DTO, BUS
Public Class frmMuonSach
    Dim dsDocGia As New List(Of DocGiaDTO)
    Dim dsSach As New List(Of SachDTO)
    Private Sub frmMuonSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim docgiaBus As New DocGiaBUS()
        Dim sachBus As New SachBUS()



        dsDocGia = docgiaBus.LayDSDocGia()
        dsSach = sachBus.LayDSSach()

        LookUpEdit_Docgia.Properties.DataSource = dsDocGia
        LookUpEdit_Docgia.Properties.ValueMember = "MaDocGia"
        LookUpEdit_Docgia.Properties.DisplayMember = "TenDocGia"
        LookUpEdit_Docgia.EditValue = 0

        LookUpEdit_Sach.Properties.DataSource = dsSach
        LookUpEdit_Sach.Properties.ValueMember = "MaSach"
        LookUpEdit_Sach.Properties.DisplayMember = "TenSach"
        LookUpEdit_Sach.EditValue = 0
    End Sub

    Private Sub SimpleButton_Thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Close()
    End Sub

    Private Sub SimpleButton_Nhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub SimpleButton_ThemSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton_ThemSach.Click
        Dim s As New SachDTO()
        For Each item In dsSach
            If item.MaSach = LookUpEdit_Sach.EditValue Then
                s = item
                Exit For
            End If
        Next
        _DataGridView_DanhSachSach.Rows.Add(s.MaSach, s.TenSach, s.MaTheLoai, s.MaTacGia, s.NamXuatBan, s.MaNhaSanXuat, s.NgayNhap, s.TriGia, s.MaTinhTrang)
    End Sub

    Private Sub SimpleButton_XoaSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton_XoaSach.Click
        Dim i As Integer = DataGridView_DanhSachSach.CurrentRow.Index
        DataGridView_DanhSachSach.Rows.RemoveAt(i)
    End Sub

    Private Sub SimpleButton_Nhap_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton_Nhap.Click

        Dim tsBus As New ThamSoBUS()
        Dim tsDto As New ThamSoDTO()
        tsDto = tsBus.LayThongtin()

        If DataGridView_DanhSachSach.Rows.Count >= tsDto.SoLuongSachMuonToiDa Then
            MessageBox.Show("Số lượng sách mượn vượt qui định !")
            Return
        End If

        Dim str As String = String.Format("{0:dd/MM/yyyy}", DateTimePicker_NgayChoMuon.Value)
        MessageBox.Show(str)

        Dim dspms As New List(Of PhieuMuonSachDTO)
        Dim pmsBUS As New PhieuMuonSachBUS()
        dspms = pmsBUS.LayDanhSachPhieuMuonSach()
        Dim sl As Integer = dspms.Count + 1

        Dim dong As Integer = DataGridView_DanhSachSach.Rows.Count - 1

        Dim sql As String = "insert into PHIEUMUONSACH (NgayMuon,MaDocGia,MaChiTietPhieuMuon)values ('"
        sql = sql & str & "'," & LookUpEdit_Docgia.EditValue.ToString() & "," & sl.ToString() & ")"

        Dim pms As New PhieuMuonSachBUS()
        Dim kq As Integer = pms.ThemPhieuMuonSach(sql)
        If kq <> 1 Then
            MessageBox.Show("Thêm phiếu mượn sách thất bại !")
            Return
        End If
        Dim ctpms As New ChiTietPhieuMuonSachBUS()

        For index As Integer = 0 To DataGridView_DanhSachSach.Rows.Count - 1
            sql = "insert into CHITIETPHIEUMUONSACH (MaChiTietPhieuMuonSach, MaSach ,MaTheLoai , MaTacGia) values ("
            sql = sql & sl.ToString() & "," & DataGridView_DanhSachSach.Rows(index).Cells(0).Value.ToString() & ", " & DataGridView_DanhSachSach.Rows(index).Cells(2).Value.ToString() & "," & DataGridView_DanhSachSach.Rows(index).Cells(3).Value.ToString() & ")"
            'MessageBox.Show(sql)
            kq = ctpms.ThemChiTietPhieuMuonSach(sql)

            If kq <> 1 Then
                MessageBox.Show("Thêm chi tiết phiếu mượn sách thất bại !")
                Return
            End If

        Next

    End Sub

    Private Sub SimpleButton_Thoat_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton_Thoat.Click
        Close()
    End Sub
End Class